// 格式化商品类目列表数据
function formatTypeList(array) {
  return array.map(item => {
    item.children = array.filter(i=>i.parent == item.id)
    return item 
  }).filter(k=>k.pid == null)
}

// 商品信息格式化
function goodsDataHandle(array = []) {
  return array.map((item) => {
    let {
      id,
      type,
      shop,
      code,
      banner,
      store_name,
      price,
      title,
      sale_mode,
      presale_start_date,
      presale_end_date,
      create_date,
      comment,
      state,
    } = item;

    let main_banner = null;
    if (banner.length) {
      let mainFindIndex = banner.findIndex((item) => Boolean(item.main));
      if (mainFindIndex != -1) {
        main_banner = banner[mainFindIndex].image;
      } else {
        main_banner = banner[0].image;
      }
    }
    return {
      id,
      type,
      shop,
      code,
      store_name,
      price: Number(price),
      title,
      main_banner,
      banner,
      sale_mode,
      presale_start_date,
      presale_end_date,
      create_date,
      comment,
      state,
    };
  });
}

module.exports = {
  formatTypeList,
  goodsDataHandle,
};
